<?php

namespace App\Http\Servers\Admin;

use App\Http\Repository\SortRepository;
use App\Info;
use App\InfoImageLines;
use App\InfoSortLines;

class InfoServer
{
    private $info;
    private $infoSortLines;
    private $infoImageLines;
    private $sortRepository;

    public function __construct(Info $info, InfoSortLines $infoSortLines, InfoImageLines $infoImageLines, SortRepository $sortRepository)
    {
        $this->info = $info;
        $this->infoSortLines = $infoSortLines;
        $this->infoImageLines = $infoImageLines;
        $this->sortRepository = $sortRepository;
    }

    //得到信息模块的模版
    public function getInfoTheme()
    {
        $tmpInfoThemes = glob(config('app.info_theme'));
        foreach ($tmpInfoThemes as $theme) {
            $infoThemes[] = explode('.', basename($theme))[0];
        }

        return $infoThemes;
    }

    //多个信息 修改分类 / 放入回收站
    public function infoDeleteAndChangeSort($th, $request, $chunk)
    {
        if ($request->isMethod('post')) {
            $th->validate($request, [
                'active' => 'required',
                'info_id' => 'required',
            ]);
            switch ($request->active) {
                case 'delinfo':
                    $result = $this->info->whereIn('id', explode(',', $request->info_id))->delete();
                    if ($result) {
                        $message['message'] = message(true, '删除成功', '信息已放入回收站!');
                    } else {
                        $message['message'] = message(false, '删除失败', '信息删除失败!');
                    }
                    return back()->with($message);
                    break;
                case 'changesort':
                    $this->infoSortLines->whereIn('info', explode(',', $request->info_id))->delete();
                    foreach (explode(',', $request->info_id) as $info) {
                        $this->infoSortLines->create(['info' => $info, 'sort' => $request->sort, 'chunk' => $chunk->id]);
                    }
                    $message['message'] = message(true, '修改分类成功', '信息分类已修改!');
                    return back()->with($message);
                    break;
            }
        }
    }

    //单个信息 放入回收站
    public function infoDelete($request, $chunk)
    {
        if ($request->has('del')) {
            $info = $this->info->findorfail($request->del);
            $result = $info->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', $chunk->name . '模块信息 ' . $info->name . ' 删除成功!');
            } else {
                $message['message'] = message(false, '删除失败', $chunk->name . '模块信息 ' . $info->name . ' 删除失败!');
            }
            return back()->with($message);
        }
    }

    //多个信息 恢复 /彻底删除
    public function infoRestoreAndForce($th, $request, $chunk)
    {
        if ($request->isMethod('post')) {
            $th->validate($request, [
                'active' => 'required',
                'info_id' => 'required',
            ]);
            switch ($request->active) {
                case 'restore':
                    $result = $this->info->withTrashed()
                        ->whereIn('id', explode(',', $request->info_id))
                        ->restore();
                    if ($result) {
                        $message['message'] = message(true, '恢复成功', $chunk->name . '模块信息恢复成功!');
                    } else {
                        $message['message'] = message(false, '恢复失败', $chunk->name . '模块信息恢复删除失败!');
                    }
                    return back()->with($message);
                    break;
                case 'force':
                    $files = $this->info->withTrashed()
                        ->whereIn('id', explode(',', $request->info_id))
                        ->lists('file');
                    $result = $this->info->withTrashed()
                        ->whereIn('id', explode(',', $request->info_id))
                        ->forceDelete();
                    if ($result) {
                        $this->infoSortLines->whereIn('info', explode(',', $request->info_id))->delete();
                        $this->infoImageLines->whereIn('info', explode(',', $request->info_id))->delete();
                        foreach ($files as $file) {
                            if (!empty($file)) {
                                unlink('.' . $file);
                            }
                        }
                        $message['message'] = message(true, '彻底删除成功', $chunk->name . '模块信息彻底删除成功!');
                    } else {
                        $message['message'] = message(false, '彻底删除失败', $chunk->name . '模块信息彻底删除失败!');
                    }
                    return back()->with($message);
                    break;
            }
        }
    }

    //单个信息 恢复
    public function infoRestore($request, $chunk)
    {
        if ($request->has('restore')) {
            $info = $this->info->withTrashed()->findorfail($request->restore);
            $result = $info->restore();
            if ($result) {
                $message['message'] = message(true, '恢复成功', $chunk->name . '模块信息 ' . $info->name . ' 恢复成功!');
            } else {
                $message['message'] = message(false, '恢复失败', $chunk->name . '模块信息 ' . $info->name . ' 恢复失败!');
            }
            return back()->with($message);
        }
    }

    //单个信息 彻底删除
    public function infoForce($request, $chunk)
    {
        if ($request->has('force')) {
            $info = $this->info->withTrashed()->findorfail($request->force);
            $result = $info->forceDelete();
            if ($result) {
                if (!empty($info->file)) {
                    unlink('.' . $info->file);
                }
                $this->infoSortLines->where('info', $request->force)->delete();
                $this->infoImageLines->where('info', $request->force)->delete();
                $message['message'] = message(true, '彻底删除成功', $chunk->name . '模块信息 ' . $info->name . ' 彻底删除成功!');
            } else {
                $message['message'] = message(false, '彻底删除失败', $chunk->name . '模块信息 ' . $info->name . ' 彻底删除失败!');
            }
            return back()->with($message);
        }
    }

    //修改信息状态
    public function infoStatusChange($request, $chunk)
    {
        if ($request->has('change')) {
            $info = \App\Info::findorfail($request->id);
            $change = $request->change;

            if (array_search($change, ['status', 'is_new', 'is_push', 'is_hot']) === false) {
                $message['message'] = message(false, '修改失败', '模块信息该属性无法修改!');
                return back()->with($message);
            }

            $info->$change = (bool)$info->$change ? 0 : 1;
            $result = $info->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', $chunk->name . '模块信息 ' . $info->name . ' 状态修改成功!');
            } else {
                $message['message'] = message(fasle, '修改失败', $chunk->name . '模块信息 ' . $info->name . ' 状态修改失败!');
            }
            return back()->with($message);
        }
    }
}